<?php

namespace App\Http\Controllers\merchant;

use App\Http\Controllers\Controller;
use App\Models\Mysql\XnMerchantBill;
use App\Models\Mysql\XnOrder;
use App\Models\Mysql\XuGoodsBrand;
use Illuminate\Http\Request;

class MerchantOrderController extends Controller
{
    //商户订单列表
    public function list(Request $request){
        $params = $request->all();
        $user = auth('merchant')->user();
        $where = [];
        $where[] = ['Merchant_number',$user->merchant_number];
        $where[] = ['recharge_time','>=',$params['start_time']];
        $where[] = ['recharge_time','<=',$params['end_time']];
        if (!empty($params['goods_name'])){
            $where[] = ['goods_name','like','%'.$params['goods_name'].'%'];
        }
        if (null != $params['recharge_status']){
            $where[] = ['recharge_status',$params['recharge_status']];
        }
        if (!empty($params['goods_id'])){
            $where[] = ['goods_id',$params['goods_id']];
        }
        if (!empty($params['account'])){
            $where[] = ['account',$params['account']];
        }

        $res = XnOrder::where($where)
            ->orderBy('recharge_time', 'desc')
            ->select('id','goods_code','account','order_sn','price','goods_num','all_money','recharge_status','recharge_time','update_time','goods_belong','msg','goods_name')
            ->paginate(10);

        if (!$res) {
            return $this->sendError('查询失败');
        }
        return $this->sendResponse($res,'查询成功');

    }
    //商户账单
    public function bill_list(Request $request){
        $params = $request->all();
        $user = auth('merchant')->user();
        $where = [];
        if (!empty($params['order_sn'])){
            $where[] = ['merchant_order',$params['order_sn']];
        }
        if (!empty($params['status'])){
            $where[] = ['status',$params['status']];
        }
        $where[] = ['time','>=',$params['start_time']];
        $where[] = ['time','<=',$params['end_time']];
        $where[] = ['merchant_number','=',$user->merchant_number];
        $res = XnMerchantBill::where($where)->orderBy('time', 'desc')->paginate(10)->toArray();
        foreach ($res['data'] as $k=>$v){
            if ($v['status'] == 1){
                $res['data'][$k]['goods_name'] = '财务加款';
            }elseif($v['status'] == 4 ){
                $res['data'][$k]['goods_name'] = '财务扣款';
            }else{
                $res['data'][$k]['goods_name'] = XnOrder::where('order_sn',$v['merchant_order'])->value('goods_name');
            }
        }
        return $this->sendResponse($res, '查询成功');
    }

    public function getordervital(Request $request){
        $start_date = $request -> post('start_date','');
        $end_date = $request -> post('end_date','');
        $search = $request -> post('search','');
        $merchant_number = $request -> post('merchant_number','');
        $condition[] = ['brand_id','>',0];
        $data = getDateFromRange($start_date,$end_date);

        if(!empty($search)){
            $brand_info = XuGoodsBrand:: where('category_name','=',$search)
                -> select('category_id','pid','level')
                -> first();
            if(!$brand_info){
                return  $this -> sendError('未查到此名称所对应的分类');
            }
            if($brand_info['level'] == 1 || $brand_info['level'] == 2){
                return  $this -> sendError('请输入三级分类');
            }
            $where[] = ['brand_id','=',$brand_info['category_id']];
        }else{
            $where[] = ['brand_id','>',0];
        }
        $res_info = [];
        foreach($data as $k => $v){
            $wheres = $where;
            $wheres[] = ['recharge_time','>=',$v.' 00:00:00'];
            $wheres[] = ['recharge_time','<=',$v.' 23:59:59'];
            $wheres[] = ['merchant_number','=',$merchant_number];
            $select = 'brand_id,count(IF(recharge_status = 1,1,NULL)) as successorder,count(IF(recharge_status = 2,1,NULL)) as failsorder,sum(IF(recharge_status = 1,all_money,null)) as successmoney,sum(if(recharge_status = 2,all_money,null)) as failsmoney';
            $res = XnOrder::where($wheres)
                ->selectRaw($select)
                ->groupBy('brand_id')
                ->get()
                ->toArray();
            if($res){
                foreach($res as $kk => $vv){
                    $res_info[] = [
                        'date' => $v,
                        'brand_id' => $vv['brand_id'],
                        'allmoney' => round(($vv['successmoney'] + $vv['failsmoney']),2),
                        'allorder' => ($vv['successorder'] + $vv['failsorder']),
                        'successorder' => $vv['successorder'],
                        'failsorder' => $vv['failsorder'],
                        'successmoney' => round($vv['successmoney'],2),
                        'failsmoney' => round($vv['failsmoney'],2),
                        'brand_name' => XuGoodsBrand::where('category_id',$vv['brand_id']) -> value('category_name')
                    ];
                }
            }
        }
       // dd($res_info);
        $last_names = array_column($res_info,'date');
        array_multisort($last_names,SORT_DESC,$res_info);
        $success_number = 0;
        $success_money = 0;
        $fail_number = 0;
        $fail_money = 0;
        $all_number = 0;
        $all_money = 0;
        foreach($res_info as $k => $v){
            $success_number+=$v['successorder'];
            $success_money+=$v['successmoney'];
            $fail_money+=$v['failsmoney'];
            $fail_number+=$v['failsorder'];
            $all_money+=$v['allmoney'];
            $all_number+=$v['allorder'];
        }
        $sum_data = [
            'success_number' => $success_number,
            'success_money' => round($success_money,2),
            'fail_money' => round($fail_money,2),
            'fail_number' => $fail_number,
            'all_money' => round($all_money,2),
            'all_number' => $all_number,
        ];

        if($res_info){
            $result['data'] = $res_info;
            $result['counts'] = $sum_data;
            return $this -> sendResponse($result,'成功');
        }else{
            return  $this -> sendError('暂无数据');
        }

    }
    public function getBillVital(Request $request){
        $start_date = $request -> post('start_date');
        $end_date = $request -> post('end_date');
        $search = $request -> post('search','');
        $merchant_number = $request -> post('merchant_number','');
        $data = getDateFromRange($start_date,$end_date);
        $res_info = [];
        if(!empty($search)){
            $brand_info = XuGoodsBrand:: where('category_name','=',$search)
                -> select('category_id','pid','level')
                -> first();
            if(!$brand_info){
                return  $this -> sendError('未查到此名称所对应的分类');
            }
            if($brand_info['level'] == 1 || $brand_info['level'] == 2){
                return  $this -> sendError('请输入三级分类');
            }
            $where[] = ['brand_id','=',$brand_info['category_id']];
        }else{
            $where[] = ['brand_id','>',0];
        }

        $where[] = ['merchant_number','=',$merchant_number];
        foreach($data as $k => $v){
            $wheres = $where;
            $wheres[] = ['time','>=',$v.' 00:00:00'];
            $wheres[] = ['time','<=',$v.' 23:59:59'];
            $infoa = XnMerchantBill:: where($wheres)  -> selectRaw('brand_id,id,time,status')-> groupBy('brand_id') -> get() -> toArray();
            if($infoa){
                foreach($infoa as $kk => $vv){
                    $res_info[] = [
                        'brand_id' => $vv['brand_id'],
                        'id' => $vv['id'],
                        'time' => $vv['time'],
                        'status' => $vv['status'],
                        'date' => $v,
                        'brand_name' => XuGoodsBrand:: where('category_id',$vv['brand_id']) -> value('category_name')
                    ];
                }
            }
        };
        $last_names = array_column($res_info,'date');
        array_multisort($last_names,SORT_DESC,$res_info);
        if($res_info){
            return successly($res_info);
        }else{
            return fails('暂无数据');
        }
    }

    public function getBillVitalDownload(Request $request){
        $timedata = $request -> get('timedata');
        $brand_id = $request -> get('brand_id');
        $brand_name = $request -> get('brand_name');
        $merchant_number = $request -> get('merchant_number');
        if(empty($timedata) || empty($brand_id)){
            return fails('缺少参数');
        }
        $wheres[] = ['recharge_time','>=',$timedata.' 00:00:00'];
        $wheres[] = ['recharge_time','<=',$timedata.' 23:59:59'];
        $wheres[] = ['brand_id','=',$brand_id];
        $wheres[] = ['merchant_number','=',$merchant_number];
        $list = XnOrder::where($wheres)
            -> selectRaw('id,order_sn,merchant_number,recharge_time,recharge_status,goods_name,account,all_money,price,goods_num,recharge_time')
            -> orderBy('recharge_time','desc')
            -> get()
            -> toArray();
        foreach($list as $k => $v){
            $list[$k]['brand_name'] = $brand_name;
        }
        return successly($list);
    }
}
